package org.haptimap.examples;

import java.util.List;

import org.haptimap.hcimodules.touchovermap.Line;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * LinesOverlay draws lines on the canvas using the projection of the given MapView.
 */
public class PointsOverlay extends Overlay {

	List<Line> lines;
	int cercleSize;
	
    public PointsOverlay(List<Line> lines, int cercleSize){
    	this.lines = lines;
    	this.cercleSize = cercleSize;
    }      
 
   
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow); 
        
        if (shadow)
        	return true;
        
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        
        for (Line s : this.lines) {
        	Point screenPts = new Point(); /*Screen point out */
        	Point screenLine = new Point();
        	Path path = new Path();
        	
        	boolean firstElement = true;
        	
        	for (Location l : s.getElements()) {
        		
        		if(l.getProvider().equals("singleNode")){
        			//---translate the GeoPoint to screen pixels---
        			mapView.getProjection().toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),(int) (l.getLongitude() * 1E6)), screenPts);
        		}
        		if(l.getProvider().equals("way")){
        			if (firstElement) {
						// first element needs move to
        				mapView.getProjection().toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),	(int) (l.getLongitude() * 1E6)), screenLine);
						path.moveTo(screenLine.x, screenLine.y);
						firstElement = false;
					}
					// others will draw lines
        			mapView.getProjection().toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),	(int) (l.getLongitude() * 1E6)), screenLine);
					path.lineTo(screenLine.x, screenLine.y);
        			
        			//---translate the GeoPoint to screen pixels---
        			//mapView.getProjection().toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),(int) (l.getLongitude() * 1E6)), screenLine);
        		}        		
        	}
        	canvas.drawPath(path, paint);
        	canvas.drawCircle(screenPts.x, screenPts.y, (float)this.cercleSize, paint);
        }
		return true;        
    }
   
}